package designPatterns.com.course_design.scrapy.spider;

import designPatterns.com.course_design.scrapy.http.request.Request;
import designPatterns.com.course_design.scrapy.http.request.ScrapyRequest;
import designPatterns.com.course_design.scrapy.http.response.Response;

import java.util.UUID;


/**
 * @projectName DesignPattern
 * @className study.scrapy.Spider
 * @description
 */

public class MySpider extends Spider implements Runnable {

    //用户自定义模块
    public void parseResponse(Response response) {
        if (responseNum == 0) return;
        String url = response.getUrl();
        int len = 2;
        for (int i = 0; i < len; i++) {
            Request request = new ScrapyRequest();
            //如果有新的request则发送到Engine
            //发送是职责链模式，要去重
            request.setUrl( "www.spider/" + (UUID.randomUUID()));
            log.info("返回新的请求:" + request.getUrl());
            //传递到职责链
            this.push(request);
        }
        responseNum--;
        //如果有数据则发送到Engine
        //此处编码未和ItemPipeline连接，故略
    }
}